<?
    require_once("objects/addrstring.php");
    require_once("objects/listing.php");
    
    class ListingManager {
        static function FetchListing($id) {
            $query = "SELECT * FROM listing_structure WHERE type <> 'header' ORDER BY location ASC;";
            $structure = DBM::FetchAllRows($query);
            $struct = array();
            $importants = array();
            $options = array();
            foreach ($structure as $row) {
                $struct[$row['id']] = $row['title'];
                if ($row['type'] == "checkbox") $options[$row['id']] = $row['options'];
                if ($row['important']) $importants[] = $row['id'];
            }
            
            $listingobj = new Listing($struct, $options);
            $listingobj->SetImportantFields($importants);
            
            $query = "SELECT * FROM listings WHERE id=$id;";
            $basicdata = DBM::FetchRow($query);
            if (!is_array($basicdata))
                ErrorMessage::Show("Unknown Listing","Could not find a listing with id: $id");
            $listingobj->SetBasicData($basicdata);
            
            $query = "SELECT * FROM listing_data WHERE listingid=$id;";
            $detaildata = DBM::FetchAllRows($query);
            foreach ($detaildata as $row) {
                $listingobj->AddDetail($row['structureid'],$row['data']);
            }
            
            return $listingobj;
        }
        
        static function CopyListing($id) {
        	$listingobj = self::FetchListing($id);
        	$update_arr = $listingobj->info;
        	
        	// remove the attributes that we absolutely know we don't want
        	unset($update_arr['id']);
        	unset($update_arr['address_line1']);
        	unset($update_arr['address_line2']);
        	
        	// need to add the details
            $query = "SELECT * FROM listing_data WHERE listingid=$id;";
            $copy_data = DBM::FetchAllRows($query);
			
			// insert the new listing
			$query = QueryTools::CreateQuery($update_arr,"listings","insert");
			DBM::Execute($query);
			$listingid = DBM::LastInsertID();
			
			foreach ($copy_data as $data) {
                unset($data['id']);
                $data['listingid'] = $listingid;
                $q = QueryTools::CreateQuery($data,"listing_data","insert");
                DBM::Execute($q);
			}
			
			return $listingid;
        }

        static function GetListings($availability = "listed") {
            $query = "SELECT * FROM listings WHERE ";
            if ($availability == "listed") {
                $dayslisted = SettingManager::GetSetting("max listing idle time");
                $query .= "listed = 1 ";
            }
            else if ($availability == "unlisted") {
                $query .= "listed = 0 ";
            }
            $query .= "AND ";


            
        }
        
        static function Exists($id) {
            return (DBM::CountRows("SELECT * FROM listings WHERE id=$id;") > 0);
        }
    }
?>
